//
//  SinaMainViewController.swift
//  SinaWeiBo
//
//  Created by 王忠楠 on 16/9/19.
//  Copyright © 2016年 wangzn. All rights reserved.
//

import UIKit

class SinaMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
        
        
        let sinaTabBar  = SinaTabBar()
        
        sinaTabBar.sinaDelegate = self
        
        sinaTabBar.composeButtonClosure = { [weak self] in
            
            
            print("我是闭包调用过来的")
        }
        
        setValue(sinaTabBar, forKey: "tabBar")
       
        //添加子视图控制器
        addChildViewController(childController: SinaHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: SinaMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: SinaDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: SinaProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
        
        
        
        
        
    
    }

   //添加子视图控制器的方法重载
    func addChildViewController(childController: UIViewController,title:String,imageName: String) {
        
        //设置tabbarItem的图片和文字
        childController.tabBarItem.image = UIImage(named: imageName)
        //修改图片的渲染方式
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        
        
        //设置文字选中的颜色
            childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        
        //设置文字大小
            childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 14)], for: .normal)
        
        //合并成一句代码显示标题
        childController.title = title
        
        //创建导航视图控制器
        let nav = SinaNavigationViewController(rootViewController:childController)
        //添加子视图控制器
        addChildViewController(nav)
        
    }
    
    
    
}

extension SinaMainViewController:SinaTabBarDelegate{
    
    func didSelectedComposeButton() {
        
        print("我是代理对象调用过来的")
    }
    
}



